//
//  XYKeychain.m
//  PeiJue
//
//  Created by lxy on 2017/10/13.
//  Copyright © 2017年 lixiya. All rights reserved.
//

#import "XYKeychain.h"
#import <UIKit/UIDevice.h>
#import <JNKeychain/JNKeychain.h>

#define kUUID   @"UUID"

@interface XYKeychain ()
@property (nonatomic, strong) NSString *uuid;
@end

@implementation XYKeychain
+ (instancetype)sharedInstance {
    static XYKeychain *keychain = nil;
    static dispatch_once_t predicate; dispatch_once(&predicate, ^{
        keychain = [[XYKeychain alloc] init];
    });
    return keychain;
}

- (BOOL)keychainUUID:(NSString *)uuid{
    return [JNKeychain saveValue:uuid forKey:kUUID];
}

#pragma mark - Publics
- (void)resetKeychainItem{
    @synchronized(self) {
        [JNKeychain deleteValueForKey:kUUID];
        _uuid = nil;
    }
}

#pragma mark - Getter
- (NSString *)uuid {
    if (!_uuid) {
        _uuid = [JNKeychain loadValueForKey:kUUID];
        if (_uuid == nil || _uuid.length <= 0) {
            NSString *UUIDString = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
            UUIDString = [UUIDString stringByReplacingOccurrencesOfString:@"-" withString:@""];
            [self keychainUUID:UUIDString];
            _uuid = UUIDString;
        }
    }
    return _uuid;
}
@end
